import 'dart:convert';

class User {
  static String tableName = 'user';
  int id;
  String uid;
  String nickname;
  String avatarUrl;
  int age;
  int sex;
  String area;
  String birthday;
  String selfIntroduction;
  String homePageBackground;
  String phoneNumber;
  String token;
  String ipAddr;
  int attentionNum;
  int fansNum;
  
  // ========== 会员相关字段 ==========
  int isAiChatMember; // 是否AI聊天会员，0为否，1为是
  String? aiChatMemberOpenTime; // AI聊天会员开通时间
  int isRealChatMember; // 是否真人聊天会员，0为否，1为是
  String? realChatMemberOpenTime; // 真人聊天会员开通时间
  // ================================

  User({
    id,
    required this.uid,
    required this.nickname,
    required this.avatarUrl,
    required this.age,
    required this.sex,
    required this.area,
    required this.birthday,
    required this.selfIntroduction,
    required this.homePageBackground,
    required this.phoneNumber,
    required this.token,
    required this.ipAddr,
    required this.attentionNum,
    required this.fansNum,
    this.isAiChatMember = 0,
    this.aiChatMemberOpenTime,
    this.isRealChatMember = 0,
    this.realChatMemberOpenTime,
  }) : id = id != null ? int.parse(id.toString()) : 0;

  factory User.fromJson(Map<String, dynamic> json) {
    return User(
      id: json['id'] ?? 0,
      uid: json['uid'] ?? '',
      nickname: json['nickname'] ?? '',
      avatarUrl: json['avatarUrl'] ?? '',
      age: json['age'] ?? 0,
      sex: json['sex'] ?? 0,
      area: json['area'] ?? '',
      birthday: json['birthday'] ?? '',
      selfIntroduction: json['selfIntroduction'] ?? '',
      homePageBackground: json['homePageBackground'] ?? '',
      phoneNumber: json['phoneNumber'] ?? '',
      token: json['token'] ?? '',
      ipAddr: json['ipAddr'] ?? '',
      attentionNum: json['attentionNum'] ?? 0,
      fansNum: json['fansNum'] ?? 0,
      isAiChatMember: json['isAiChatMember'] ?? 0,
      aiChatMemberOpenTime: json['aiChatMemberOpenTime'],
      isRealChatMember: json['isRealChatMember'] ?? 0,
      realChatMemberOpenTime: json['realChatMemberOpenTime'],
    );
  }

  factory User.fromJsonString(String jsonString) {
    return User.fromJson(jsonDecode(jsonString));
  }

  Map<String, dynamic> toJson() {
    return {
      'id': id,
      'uid': uid,
      'nickname': nickname,
      'avatarUrl': avatarUrl,
      'age': age,
      'sex': sex,
      'area': area,
      'birthday': birthday,
      'selfIntroduction': selfIntroduction,
      'homePageBackground': homePageBackground,
      'phoneNumber': phoneNumber,
      'token': token,
      'ipAddr': ipAddr,
      'attentionNum': attentionNum,
      'fansNum': fansNum,
      'isAiChatMember': isAiChatMember,
      if (aiChatMemberOpenTime != null) 'aiChatMemberOpenTime': aiChatMemberOpenTime,
      'isRealChatMember': isRealChatMember,
      if (realChatMemberOpenTime != null) 'realChatMemberOpenTime': realChatMemberOpenTime,
    };
  }

  // ========== 会员状态检查方法 ==========
  
  /// 是否是AI聊天会员（月会员）
  bool get hasAiChatMember {
    return isAiChatMember == 1;
  }

  /// 是否是真人聊天会员（年会员）
  bool get hasRealChatMember {
    return isRealChatMember == 1;
  }

  /// 是否是任何类型的会员
  bool get isMember {
    return hasAiChatMember || hasRealChatMember;
  }

  /// 获取会员类型描述
  String get memberTypeDesc {
    if (hasAiChatMember && hasRealChatMember) return 'AI聊天会员 + 真人聊天会员';
    if (hasAiChatMember) return 'AI聊天会员';
    if (hasRealChatMember) return '真人聊天会员';
    return '普通用户';
  }

  /// 获取AI聊天会员开通时间描述
  String get aiChatMemberOpenTimeDesc {
    if (aiChatMemberOpenTime == null) return '未开通';
    return '开通时间: $aiChatMemberOpenTime';
  }

  /// 获取真人聊天会员开通时间描述
  String get realChatMemberOpenTimeDesc {
    if (realChatMemberOpenTime == null) return '未开通';
    return '开通时间: $realChatMemberOpenTime';
  }
  // ====================================

  String toJsonString() {
    return jsonEncode(toJson());
  }
}
